package com.fubang.greendaolearning;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者: YIN
 * 创建时间: 2017/8/4 10:24
 * 电子邮箱: yin_ruimin@foxmail.com
 * 描述: Note列表适配器
 */

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyViewHolder> {

    private OnNoteClickListener listener;
    private List<Note> dataList;

    public NoteAdapter(OnNoteClickListener listener) {
        this.listener = listener;
        dataList = new ArrayList<>();
    }

    /**
     * @param noteList 设置一个Note列表
     */
    public void SetNote(List<Note> noteList) {
        dataList = noteList;
        notifyDataSetChanged();
    }

    /**
     * @param position 位置
     * @return 返回指定位置的Note对象
     */
    public Note getNote(int position) {
        return dataList.get(position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_note, null);
        return new MyViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Note note = dataList.get(position);
        holder.mTextViewNoteText.setText(note.getText());
        holder.mTextViewNoteComment.setText(note.getComment());
    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnNoteClickListener {
        void onNoteClick(int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.textViewNoteText)
        TextView mTextViewNoteText;
        @BindView(R.id.textViewNoteComment)
        TextView mTextViewNoteComment;

        public MyViewHolder(View itemView, final OnNoteClickListener listener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onNoteClick(getAdapterPosition());
                    }
                }
            });

        }
    }
}
